package com.lzb.play.dialog;

import android.content.Context;
import android.media.AudioManager;
import android.net.Uri;
import android.view.Gravity;
import android.view.View;
import android.widget.SeekBar;

import androidx.annotation.NonNull;

import com.blankj.utilcode.util.AppUtils;
import com.blankj.utilcode.util.UriUtils;
import com.blankj.utilcode.util.VolumeUtils;
import com.lzb.play.R;
import com.lzb.play.basic.dialog.BaseSmartDialog;
import com.lzb.play.basic.media.AudioPlayerImpl;
import com.lzb.play.basic.media.IAudioPlayer;
import com.lzb.play.bean.HomeBirdBean;
import com.lzb.play.databinding.DialogBirdInfoBinding;
import com.lzb.play.databinding.DialogVolumeSetBinding;

public class BirdInfoDialog extends BaseSmartDialog<DialogBirdInfoBinding> implements View.OnClickListener, IAudioPlayer.IListener {

    private HomeBirdBean homeBirdBean;
    private IAudioPlayer mAudioPlayer;

    public BirdInfoDialog(@NonNull Context context) {
        super(context);
    }

    public void setHomeBirdBean(HomeBirdBean homeBirdBean) {
        this.homeBirdBean = homeBirdBean;
    }

    @Override
    protected int getGravity() {
        return Gravity.CENTER;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.dialog_bird_info;
    }

    @Override
    protected void initView(View view) {
        mDataBinding.ivCancel.setOnClickListener(this);
        mDataBinding.ivPlay.setOnClickListener(this);

        mDataBinding.ivImg.setImageResource(homeBirdBean.getDescImg());
        mDataBinding.tvName.setText(homeBirdBean.getBirdBean().getName());
        mDataBinding.tvAlias.setText(homeBirdBean.getBirdBean().getAlias());
        mDataBinding.tvDesc.setText(homeBirdBean.getBirdBean().getDescription());
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.ivCancel) {
            dismiss();
        } else if (id == R.id.ivPlay) {
            if (mAudioPlayer == null) {
                mAudioPlayer = new AudioPlayerImpl();
                mAudioPlayer.setListener(this);
                Uri uri = Uri.parse("android.resource://" + AppUtils.getAppPackageName() + "/" + homeBirdBean.getRawId());
                mAudioPlayer.play(UriUtils.uri2File(uri).getPath());
                return;
            }
            if (mAudioPlayer.isPlaying()) {
                mAudioPlayer.pause();
            } else {
                mAudioPlayer.resume();
            }
        }
    }

    @Override
    public void onUpdatePlayTime(int curPlayTime, int duration) {

    }

    @Override
    public void onPlayChange(boolean isPlaying) {
        if (isPlaying) {
            mDataBinding.ivPlay.setImageResource(R.drawable.stop);
        } else {
            mDataBinding.ivPlay.setImageResource(R.drawable.play);
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        if (mAudioPlayer != null) {
            mAudioPlayer.pause();
        }
    }

    private Ilistener listener;

    public void setListener(Ilistener listener) {
        this.listener = listener;
    }

    public interface Ilistener {
        void click(String name);
    }
}
